package com.example.swd1.gudong.main.usermanage.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.constant.Constant;
import com.example.swd1.gudong.constant.MyCookieStore;
import com.example.swd1.gudong.main.MainActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG ="LoginActivity";
    private EditText userNameEdit,passwordEdit;
    private Button signInBtn;
    private TextView registerBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        userNameEdit = (EditText) findViewById(R.id.user_name_edit);
        passwordEdit = (EditText) findViewById(R.id.password_edit);
        signInBtn = (Button) findViewById(R.id.sign_in_btn);
        registerBtn = (TextView) findViewById(R.id.register_btn);

        signInBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sign_in_btn:
                if(checkEditorForm()){
                    signIn();
                }else {
                    Toast.makeText(LoginActivity.this,"用户名或密码不能空！",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.register_btn:
                break;
        }
    }

    private boolean checkEditorForm() {
        if(!TextUtils.isEmpty(userNameEdit.getText().toString())&&!TextUtils.isEmpty(passwordEdit.getText().toString())){
            return true;
        }else
            return false;
    }

    private void signIn() {

//        AsyncHttpClient client = new AsyncHttpClient();

        final HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("username",userNameEdit.getText().toString());
        params.addQueryStringParameter("password",passwordEdit.getText().toString());


        httpUtils.send(HttpRequest.HttpMethod.GET, Constant.LOGIN_URL,params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Log.d( TAG,"success:" + ","+responseInfo.result);
                        if (responseInfo.result.equals("succeed")){
                            DefaultHttpClient dh = (DefaultHttpClient) httpUtils.getHttpClient();
                            MyCookieStore.cookieStore = dh.getCookieStore();
                            CookieStore cs = dh.getCookieStore();
                            List<Cookie> cookies = cs.getCookies();
                            for (int i = 0; i < cookies.size(); i++) {
                                Log.d(TAG,cookies.get(i).getName()+":"+cookies.get(i).getValue());
                                if ("JSESSIONID".equals(cookies.get(i).getName())) {
                                    MyCookieStore.SESSIONID = cookies.get(i).getValue();
                                    break;
                                }
                            }
                            Toast.makeText(LoginActivity.this,"登陆成功！",Toast.LENGTH_LONG).show();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));

                        }else if(responseInfo.result.equals("failed")){
                            Toast.makeText(LoginActivity.this,"登陆失败，请检查用户名或密码后再次登陆！",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Log.d( TAG,"failure:" + "," + s);
                        Toast.makeText(LoginActivity.this,"登陆失败，请检查网络或稍后尝试！",Toast.LENGTH_LONG).show();
//                        testText.setText("failure:" + "," + s);
                    }
                });
    }
}
